/*
 *代码由框架生成,任何更改都可能导致被代码生成器覆盖
 *Repository提供数据库操作，如果要增加数据库操作请在当前目录下Partial文件夹SalesReturnOrderItemModelRepository编写代码
 */
using VOL.demo.IRepositories;
using VOL.Core.BaseProvider;
using VOL.Core.DbContext;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using SqlSugar;

namespace VOL.demo.Repositories
{
    public partial class SalesReturnOrderItemModelRepository : RepositoryBase<SalesReturnOrderItemModel>
    , ISalesReturnOrderItemModelRepository
    {
        public SalesReturnOrderItemModelRepository(VOLContext dbContext)
        : base(dbContext)
        {

        }
        public static ISalesReturnOrderItemModelRepository Instance
        {
            get { return AutofacContainerModule.GetService<ISalesReturnOrderItemModelRepository>(); }
        }

        /// <summary>
        /// 根据退货单ID获取明细列表
        /// </summary>
        public async Task<List<SalesReturnOrderItemModel>> GetItemsByReturnOrderIdAsync(long returnOrderId)
        {
            return await DbContext.Queryable<SalesReturnOrderItemModel>()
                .Where(item => item.returnOrderId == returnOrderId && item.isDel == 0)
                .ToListAsync();
        }

        /// <summary>
        /// 批量添加退货明细
        /// </summary>
        public async Task<bool> AddItemsAsync(List<SalesReturnOrderItemModel> items)
        {
            if (items == null || !items.Any())
            {
                return false;
            }

            var result = await DbContext.Insertable(items).ExecuteCommandAsync();
            return result > 0;
        }

        /// <summary>
        /// 根据退货单ID删除明细
        /// </summary>
        public async Task<bool> DeleteItemsByReturnOrderIdAsync(long returnOrderId)
        {
            var result = await DbContext.Updateable<SalesReturnOrderItemModel>()
                .SetColumns(item => item.isDel == 1)
                .Where(item => item.returnOrderId == returnOrderId)
                .ExecuteCommandAsync();
            return result > 0;
        }

        /// <summary>
        /// 更新明细项
        /// </summary>
        public async Task<bool> UpdateItemAsync(SalesReturnOrderItemModel item)
        {
            var result = await DbContext.Updateable(item).ExecuteCommandAsync();
            return result > 0;
        }
    }
}



